package org.example.controller;

import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.example.dto.StudentDto;
import org.example.dto.TeacherDto;
import org.example.service.StudentService;
import org.example.service.TestService;
import org.example.utils.JsonUtils;
import org.example.vo.StudentVo;
import org.example.vo.TeacherVo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Rongchang Wu
 * @version 1.0
 * @description: TODO
 * @date 2021/12/10 9:01
 */
@RestController
public class StudentController {
    @Resource
    StudentService service;

    @GetMapping("/getStudentByName")
    @ResponseBody
    public void getStudentByName(HttpServletResponse resp, String studentName) throws IOException {
        StudentDto student = service.queryStudentByName(studentName);
        student.setAge(DateUtil.ageOfNow(student.getBirthday()));
        student.setUnionAddress(student.getProvince() + student.getCity() + student.getDistrict() + student.getAddress());
        //以上service根据系别名查找，返回对应的系
        List<StudentDto> studentList = new ArrayList<>();
        studentList.add(student);

        JsonUtils.customJson(resp, studentList, studentList.size());
    }

    @GetMapping("/getStudentByDepartmentAndMajorAndGradeAndClass")
    @ResponseBody
    public void getStudentByDepartmentAndMajorAndGradeAndClass(HttpServletResponse resp, String departmentName, String majorName, String gradeName, String classesName) throws IOException {
        List<StudentDto> studentList = service.queryStudentByDepartmentAndMajorAndGradeAndClass(departmentName, majorName, gradeName, classesName);
        Integer count = service.countStudentByDepartmentAndMajorAndGradeAndClass(departmentName, majorName, gradeName, classesName);
        //以上service根据系别名查找，返回对应的系

        JsonUtils.customJson(resp, studentList, count);
    }

    @GetMapping("/getStudentList")
    @ResponseBody
    public void getStudentList(HttpServletResponse resp, Integer page, Integer limit) throws IOException {
        System.out.println("查询学生");
        Page<Object> obj = PageHelper.startPage(page, limit);
        List<StudentDto> studentList = service.queryAllStudent();
        for (int i = 0; i < studentList.size(); i++) {
            StudentDto student = studentList.get(i);
            student.setAge(DateUtil.ageOfNow(student.getBirthday()));
            student.setUnionAddress(student.getProvince() + student.getCity() + student.getDistrict() + student.getAddress());
        }
        Integer count = service.countStudent();
        //以上调用service查找所有系别，赋值给studentList；查找系别数量，赋值给count

        JsonUtils.customJson(resp, studentList, count);
        System.out.println("查询学生");
    }

    @GetMapping(value = "/deleteStudent", produces = "text/plain;charset=utf-8")
    @ResponseBody
    public String deleteStudent(String studentId) {
        String[] studentIds = studentId.split(",");
        Integer status = 0;
        for (int i = 0; i < studentIds.length; i++) {
            status += service.deleteStudent(Integer.parseInt(studentIds[i]));
        }
        //以上调用service方法删除系别

        return status > 0 ? "success" : "error";
    }

    @GetMapping(value = "/addStudent", produces = "text/plain;charset=utf-8")
    @ResponseBody
    public String addStudent(StudentVo student) {
        Integer status = service.addStudent(student);
        //以上调用service方法添加系别

        return status > 0 ? "success" : "error";
    }

    @GetMapping("/getStudentById")
    @ResponseBody
    public StudentDto getStudentById(Integer studentId) {
        StudentDto student = service.queryStudentById(studentId);
        //以上service根据系系号查找，返回对应的系

        return student;
    }

    @GetMapping("/editStudent")
    @ResponseBody
    public String editStudent(StudentVo student) {
        System.out.println("进入/editStudent");
        System.out.println(student);
        Integer status = service.updateStudent(student);
        //以上调用service方法修改系别

        return status > 0 ? "success" : "error";
    }
}
